package com.castle.fortress.admin.cms.fragment;

import cn.hutool.core.util.StrUtil;
import com.castle.fortress.admin.cms.dto.CmsArticleDto;
import com.castle.fortress.admin.cms.service.CmsArticleService;
import com.castle.fortress.admin.utils.SpringUtils;
import org.beetl.core.tag.GeneralVarTagBinding;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * cms 用户自定义标签文章列表
 * @author castle
 */
public class CmsTagArticleListTag extends GeneralVarTagBinding {

	@Override
	public void render() {
		CmsArticleService cmsArticleService = SpringUtils.getBean(CmsArticleService.class);
		List<CmsArticleDto> list =new ArrayList<>();
		Map<String,Object> map = new HashMap<>();
		//指定自定义标签 多个标签用逗号隔开
		if(containHtmlAttribute("code")){
			map.put("code",getAttributeValue("code"));
		}
		//排序字段 orderby：self(本级)、all(本级及子集)、child(子集) | 排序方式 orderway：desc asc
		if(containHtmlAttribute("orderby")  && containHtmlAttribute("orderway")){
			if(("id".equals(getAttributeValue("orderby").toString())
			|| "pubdate".equals(getAttributeValue("orderby").toString())
			|| "click".equals(getAttributeValue("orderby").toString())
					|| "sort".equals(getAttributeValue("orderby").toString())
			)&&("asc".equals(getAttributeValue("orderway").toString())||"desc".equals(getAttributeValue("orderway").toString()))){
				map.put("orderby",getAttributeValue("orderby"));
				map.put("orderway",getAttributeValue("orderway"));
			}
		}
		//标题长度限制
		if(containHtmlAttribute("titlelen")&& StrUtil.isNotEmpty(getAttributeValue("titlelen").toString())){
			try {
				Integer titlelen = Integer.valueOf(getAttributeValue("titlelen").toString());
				if(titlelen>0){
					map.put("titlelen",titlelen);
				}
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
		}
		//内容摘要长度
		if(containHtmlAttribute("infolen")&&StrUtil.isNotEmpty(getAttributeValue("infolen").toString())){
			try {
				Integer infolen = Integer.valueOf(getAttributeValue("infolen").toString());
				if(infolen>0){
					map.put("infolen",infolen);
				}
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
		}
		//文档属性(系统预设标签) 多个栏目用逗号隔开
		if(containHtmlAttribute("flag")){
			map.put("flag",getAttributeValue("flag"));
		}
		//过滤文章数目
		if(containHtmlAttribute("row")&&StrUtil.isNotEmpty(getAttributeValue("row").toString())){
			try {
				Integer rows = Integer.valueOf(getAttributeValue("row").toString());
				map.put("row",rows);
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
		}
		Integer start=0;
		if(map.get("start")!=null&&StrUtil.isNotEmpty(map.get("start").toString())){
			start = Integer.valueOf(map.get("start").toString());
		}
		//分页显示
		if(containHtmlAttribute("pagesize")&&containHtmlAttribute("pageindex")
				&&StrUtil.isNotEmpty(getAttributeValue("pagesize").toString())
				&&StrUtil.isNotEmpty(getAttributeValue("pageindex").toString())){
			try {
				Integer pagesize = Integer.valueOf(getAttributeValue("pagesize").toString());
				Integer pageindex = Integer.valueOf(getAttributeValue("pageindex").toString());
				if(pagesize>0&&pageindex>=1){
					Integer limitstart = (pageindex-1)*pagesize+start;
					map.put("limitstart",limitstart);
					map.put("limitrow",pagesize);
				}

			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
		}
		list = cmsArticleService.cmsTagTagCmsArticle(map);
		if(map.get("limitstart")!=null && map.get("limitrow")!=null){
			Integer total = cmsArticleService.cmsTagCountTagCmsArticle(map);
			if(total>=start){
				total=total-start;
			}else{
				total=0;
			}
			Integer pagesize = Integer.parseInt(map.get("limitrow").toString());
			//总页数
			Integer totalpage = total%pagesize==0?(total/pagesize):((total/pagesize)+1);
			//绑定对象 列表、总页数、总记录数
			this.binds(list,totalpage,total);
		}else{
			this.binds(list);
		}
		this.doBodyRender();
	}
}
